#include "ControlUI.h"
#include "BkgLayer.h"
#include "GlobalMgr.h"

USING_NS_CC;
//////////////////////////////////////////
//SpeedUpBtn
////////////////////////////////////////////////////
bool SpeedUpBtn::init()
{
	if ( !CCSprite::init() )
    {
        return false;
    }

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("menuitemsprite.png");
	setTexture(texture);
	setTextureRect(CCRect(0, 46, 115, 23));
		
	return true;
}

void SpeedUpBtn::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCSprite::onEnter();
}

void SpeedUpBtn::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCSprite::onExit();
}

bool SpeedUpBtn::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	if (CCRect(55, 89, 100, 23).containsPoint(touch->getLocation()))
	{
		setTextureRect(CCRect(0, 23, 115, 23));
		if (GlobalMgr::getInstance().m_bCrazyStyle)
		{
			return true;
		}
		BkgLayer::m_pSheep->setSpeed(2);
		((BkgLayer*)m_pParent)->setPowerBar(-1);
		return true;
	}
	
	return false;
}

void SpeedUpBtn::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}

void SpeedUpBtn::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	setTextureRect(CCRect(0, 46, 115, 23));
	if (!GlobalMgr::getInstance().m_bCrazyStyle)
	{
		BkgLayer::m_pSheep->setSpeed(1);
		((BkgLayer*)m_pParent)->stopPowerBar();
	}
}

//////////////////////////////////////////
//SavePowerBtn
////////////////////////////////////////////////////
bool SavePowerBtn::init()
{
	if ( !CCSprite::init() )
    {
        return false;
    }

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("menuitemsprite.png");
	setTexture(texture);
	setTextureRect(CCRect(0, 46, 115, 23));
		
	return true;
}

void SavePowerBtn::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCSprite::onEnter();
}

void SavePowerBtn::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCSprite::onExit();
}

bool SavePowerBtn::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	GlobalMgr& mgr = GlobalMgr::getInstance();

	if (CCRect(255, 89, 100, 23).containsPoint(touch->getLocation()))
	{
		setTextureRect(CCRect(0, 23, 115, 23));
		if (mgr.m_bCrazyStyle || !mgr.m_bStarted)
		{
			return true;
		}	
		BkgLayer::m_pSheep->setSpeed(0.5);
		((BkgLayer*)m_pParent)->setPowerBar(1);
		
		return true;
	}
	
	return false;
}

void SavePowerBtn::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}

void SavePowerBtn::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	setTextureRect(CCRect(0, 46, 115, 23));
	if (!GlobalMgr::getInstance().m_bCrazyStyle)
	{
		BkgLayer::m_pSheep->setSpeed(1);
		((BkgLayer*)m_pParent)->stopPowerBar();
	}
}
//////////////////////////////////////////
//ResetBtn
////////////////////////////////////////////////////
bool ResetBtn::init()
{
	if ( !CCSprite::init() )
    {
        return false;
    }

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("menuitemsprite.png");
	setTexture(texture);
	setTextureRect(CCRect(0, 46, 115, 23));
		
	return true;
}

void ResetBtn::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCSprite::onEnter();
}

void ResetBtn::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCSprite::onExit();
}

bool ResetBtn::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	if (CCRect(455, 89, 100, 23).containsPoint(touch->getLocation()))
	{
		((BkgLayer*)m_pParent)->resetMap();
		
		return true;
	}
	
	return false;
}

void ResetBtn::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}

void ResetBtn::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	setTextureRect(CCRect(0, 46, 115, 23));
	BkgLayer::m_pSheep->setSpeed(1);
	((BkgLayer*)m_pParent)->stopPowerBar();
}
//////////////////////////////////////////
//BombBtn
////////////////////////////////////////////////////
bool BombBtn::init()
{
	if ( !CCSprite::init() )
    {
        return false;
    }

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("menuitemsprite.png");
	setTexture(texture);
	setTextureRect(CCRect(0, 46, 115, 23));
		
	return true;
}

void BombBtn::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCSprite::onEnter();
}

void BombBtn::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCSprite::onExit();
}

bool BombBtn::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	if (CCRect(655, 89, 100, 23).containsPoint(touch->getLocation()))
	{
		setTextureRect(CCRect(0, 23, 115, 23));
	
		((BkgLayer*)m_pParent)->bombing();
		
		return true;
	}
	
	return false;
}

void BombBtn::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}

void BombBtn::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	setTextureRect(CCRect(0, 46, 115, 23));
}

//////////////////////////////////////////
//CrazyBtn
////////////////////////////////////////////////////
bool CrazyBtn::init()
{
	if ( !CCSprite::init() )
    {
        return false;
    }

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("menuitemsprite.png");
	setTexture(texture);
	setTextureRect(CCRect(0, 46, 115, 23));
		
	return true;
}

void CrazyBtn::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCSprite::onEnter();
}

void CrazyBtn::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCSprite::onExit();
}

bool CrazyBtn::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	GlobalMgr& mgr = GlobalMgr::getInstance();
		
	if (CCRect(55, 119, 100, 23).containsPoint(touch->getLocation()))
	{
		setTextureRect(CCRect(0, 23, 115, 23));
		if (!mgr.m_bStarted)
		{
			return true;
		}	
		
		mgr.m_bCrazyStyle = true;
		((BkgLayer*)m_pParent)->m_pSheep->setSpeed(3);
		
		return true;
	}
	
	return false;
}

void CrazyBtn::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}

void CrazyBtn::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	setTextureRect(CCRect(0, 46, 115, 23));
}

//////////////////////////////////////////
//GodBtn
////////////////////////////////////////////////////
bool GodBtn::init()
{
	if ( !CCSprite::init() )
    {
        return false;
    }

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("menuitemsprite.png");
	setTexture(texture);
	setTextureRect(CCRect(0, 46, 115, 23));
		
	return true;
}

void GodBtn::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCSprite::onEnter();
}

void GodBtn::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCSprite::onExit();
}

bool GodBtn::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	if (CCRect(255, 119, 100, 23).containsPoint(touch->getLocation()))
	{
		setTextureRect(CCRect(0, 23, 115, 23));
	
		((BkgLayer*)m_pParent)->setGodStyle();

		return true;
	}
	
	return false;
}

void GodBtn::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
}

void GodBtn::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	setTextureRect(CCRect(0, 46, 115, 23));
}

////////////////////////////////////////////////////
//PowerBarCtrl
////////////////////////////////////////////////////
PowerBarCtrl::PowerBarCtrl(BkgLayer* pBkgLayer) : m_nMaxBar(2), m_nFulBar(0), m_pParBkgLayer(pBkgLayer)
												 ,m_nCurBar(0)
{
	initBars();
}

PowerBarCtrl::~PowerBarCtrl()
{
}

void PowerBarCtrl::initBars()
{
	for (int i = 0; i < m_nMaxBar; ++i)
	{
		m_pPowerBars[i] = PowerBar::create();
		m_pPowerBars[i]->setPosition(ccp(50, 600 - i * 100));
		m_pParBkgLayer->addChild(m_pPowerBars[i], UI_TAG); 
	}
}

void PowerBarCtrl::startSavPw(int nAdd)
{
	m_nAdd = nAdd;
	schedule(schedule_selector(PowerBarCtrl::setPower));
}

void PowerBarCtrl::stopSavPw()
{
	unschedule(schedule_selector(PowerBarCtrl::setPower));
}

void PowerBarCtrl::setPower(float dt)
{
	float fPer = m_pPowerBars[m_nCurBar]->setPowerPer(m_nAdd);
	
	if (fPer > 1)
	{
		if (m_nFulBar == m_nMaxBar)
		{
			return;
		}
		
		if (m_nFulBar < m_nMaxBar - 1)
		{
			++m_nCurBar;
		}
	   	++m_nFulBar;
	}
	else if (fPer < 0)
	{
		if (m_nFulBar == 0)
		{
			unschedule(schedule_selector(PowerBarCtrl::setPower));
			BkgLayer::m_pSheep->setSpeed(1);
			return;
		}	
		
		--m_nFulBar;
		--m_nCurBar;
	}
	else if (m_nCurBar == m_nMaxBar - 1)
	{
		m_nFulBar = m_nMaxBar - 1;
	}
}

bool PowerBarCtrl::bombingOrGodStyle()
{
	if (m_nFulBar > 0)
	{
		m_pPowerBars[m_nCurBar-1]->m_fPer = m_pPowerBars[m_nCurBar]->m_fPer;
		m_pPowerBars[m_nCurBar]->m_fPer = 0;
		m_pPowerBars[m_nCurBar]->setPowerPer(0);
		if (m_nFulBar < m_nMaxBar)
		{
			--m_nCurBar;
		}
		--m_nFulBar;
		m_pPowerBars[m_nCurBar]->setPowerPer(0);

		return true;
	}

	return false;
}

////////////////////////////////////////////////////
//PowerBar
////////////////////////////////////////////////////
PowerBar::PowerBar() : m_nAdd(0), m_fPer(0)
{
	
}

bool PowerBar::init()
{
	if ( !CCSprite::init() )
    {
        return false;
    }

	initWithFile("f1.png");
	setTextureRect(CCRect(0,0,0,92));
	setAnchorPoint(ccp(0, 0.5));
	
	return true;
}

float PowerBar::setPowerPer(int nIsAdd)                //nIsAdd 只取 + - 1   
{
	CCRect rcTex = getTextureRect();
	
	if ((m_fPer >= 0 && m_fPer <= 1) || (nIsAdd == -1 && m_fPer > 1) || (nIsAdd == 1 && m_fPer <0))
	{
		m_fPer += 0.01 * nIsAdd;
		rcTex.setRect(0, 0, m_pobTexture->getPixelsWide() * m_fPer, rcTex.getMaxY() - rcTex.getMinY());
		setTextureRect(rcTex);
	}

	return m_fPer;
}